package com.powerunion.datacollection.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.Constants;
import com.powerunion.datacollection.dao.CategoryRolesDao;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.model.CategoryRoles;
import com.powerunion.datacollection.persistence.db.jdbc.operator.QueryOperator;
import com.powerunion.datacollection.persistence.db.jdbc.operator.RsDataSet;
import com.powerunion.datacollection.persistence.db.jdbc.operator.SqlParameter;
import com.powerunion.datacollection.persistence.db.jdbc.operator.SqlParameterSet;
import com.powerunion.datacollection.persistence.db.jdbc.operator.SqlType;
import com.powerunion.datacollection.persistence.db.jdbc.operator.UpdateOperator;
import com.powerunion.util.log.LogHelper;

public class CategoryRolesDaoImpl extends JdbcBase implements CategoryRolesDao{
	// 初始化日志类
	private static final Logger logger = LogHelper.getInstance().getLogger(
			CategoryRolesDaoImpl.class);
	
	public boolean save(CategoryRoles cr,String[]roleIds) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug(Constants.INTOMETHOD + "save");
		}
		
		String sql = "insert into dc_category_roles(id, category_id,role_id,category_type,permission_type) values(?,?,?,?,?)";

		UpdateOperator update = new UpdateOperator();
		update.setSql(sql);
		
		for(int i=0;i<roleIds.length;i++){
			SqlParameterSet spSet = new SqlParameterSet();
			String roleId = (String)roleIds[i];
			String id = String.valueOf(this.getSeq("dc_category_roles"));
			spSet.addParameter(new SqlParameter(SqlType.VARCHAR,id));
			spSet.addParameter(new SqlParameter(SqlType.VARCHAR,cr.getCategoryId()));
			spSet.addParameter(new SqlParameter(SqlType.VARCHAR,roleId));
			spSet.addParameter(new SqlParameter(SqlType.INTEGER,new Integer(cr.getCategoryType())));
			spSet.addParameter(new SqlParameter(SqlType.INTEGER,new Integer(cr.getPermissonType())));
			
			update.addBatchParameter(spSet);
		}
		try {
			update.access();
			
		} catch (Exception e) {
			logger.error("save:" + Constants.SAVE + "role" + Constants.FAIL,e);
			throw new  DaoException("保存类别权限失败！");
		}
		
		int[] effectCount = update.getBatchEffectCounts();
		if(effectCount != null && effectCount.length>0) {
			if (logger.isDebugEnabled()) {
				logger.debug("save:" + Constants.SAVE + "类别权限信息"
						+ Constants.SUCCESS);
			}
			return true;
		}else{
			if (logger.isDebugEnabled()) {
				logger.debug("save:" + Constants.SAVE + "类别权限信息"
						+ Constants.FAIL);
			}
			return false;
		}

	}

	public boolean delete(String code,int permissionType) throws DaoException {
		logger.debug(Constants.INTOMETHOD + "delete");
		String sql = "delete from dc_category_roles where category_id=? and permission_type=?";
		UpdateOperator update = new UpdateOperator();
		update.setSql(sql);
		update.addParameter(new SqlParameter(SqlType.VARCHAR,code));
		update.addParameter(new SqlParameter(SqlType.INTEGER,new Integer(permissionType)));
		try{
			update.access();
		}catch(Exception e){
			logger.error("删除类别ID为:"+code+" 的类别权限信息失败！", e);
			throw new DaoException("删除类别ID为:"+code+" 的类别权限信息失败！");
		}
		
		int effectCount = update.getEffectCounts();
		if(effectCount >0) {
			if (logger.isDebugEnabled()) {
				logger.debug("updateState:" + Constants.DELETE + "ID为:"+code+" 的类别权限信息"
						+ Constants.SUCCESS);
			}
			return true;
		}else{
			if (logger.isDebugEnabled()) {
				logger.debug("updateState:" + Constants.DELETE + "ID为:"+code+" 的类别权限信息"
						+ Constants.FAIL);
			}
			return false;
		}
		
	}

	public List getRolesByCatetoryId(String categoryId,int permissionType) throws DaoException {
		StringBuffer sql = new StringBuffer();
		sql.append("select role_id from dc_category_roles where category_id=? ")
		   .append(" and permission_type = ?");
		QueryOperator query = new QueryOperator();
		query.setSql(sql.toString());
		query.addParameter(new SqlParameter(SqlType.VARCHAR,categoryId));
		query.addParameter(new SqlParameter(SqlType.INTEGER,new Integer(permissionType)));
		List roles = null;
		try{
			roles = new ArrayList();
			query.access();
			if(query.resultSetAvailable()){
				//获取并处理记录集
				RsDataSet rs = new RsDataSet(query.getSqlResultSet());
				for(int i=0;i<rs.rowCount;i++){					
					String roleId = rs.getFieldValueAsString("role_id");
					roles.add(roleId);
					
					rs.next();
				}

				rs.clearAll();
			}
		}catch(Exception e){
			logger.error("查询类别ID为："+categoryId+" 的角色信息出错！", e);
			throw new DaoException("查询类别ID为："+categoryId+" 的角色信息出错！");
		}
		return roles;
	}
}
